package com.goldplusgold.tradedelay.app.home.live.adapter;

import android.content.Context;
import android.content.Intent;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.goldplusgold.live.LVDataManager;
import com.goldplusgold.live.bean.VideoBean;
import com.goldplusgold.live.result.ResultVideos;
import com.goldplusgold.imageloader.lib.IShow;
import com.goldplusgold.support.lib.base.activity.BaseActivity;
import com.goldplusgold.support.lib.base.activity.BaseNetWorkActivity;
import com.goldplusgold.support.lib.base.adapter.BaseListAdapter;
import com.goldplusgold.support.lib.widget.pulltorefresh.PullToRefreshListView;
import com.goldplusgold.tradedelay.app.R;
import com.goldplusgold.tradedelay.app.home.live.LiveDetailActivity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/7/10.
 */

public class VideoListAdatper extends  BaseListAdapter<List<VideoBean>> {

    private List<VideoBean> datas = new ArrayList<>();
    private int offset = 0;
    private int count = 10;
    volatile int tryCount = 0;
    PullToRefreshListView listView;
    public VideoListAdatper(){

    }

    @Override
    public void setContext(Context context) {
        super.setContext(context);
    }

    @Override
    public boolean areAllItemsEnabled() {
        return true;
    }

    @Override
    public boolean isEnabled(int position) {
        return false;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public int getCount() {
        if(datas != null){
            return datas.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(datas != null){
            datas.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.video_item_layout, null);
            holder = new ViewHolder();
            holder.control = (ImageView) convertView.findViewById(R.id.video_item_layout_img_control);
            holder.title = (TextView) convertView.findViewById(R.id.video_item_layout_txt_title);
            holder.time = (TextView) convertView.findViewById(R.id.video_item_layout_txt_time);
            holder.back = (ImageView) convertView.findViewById(R.id.video_item_layout_img_back);
            holder.rootView = convertView;
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        bindData(holder,position);
        return convertView;
    }

    private void bindData(ViewHolder holder,final int position){
        holder.time.setText(datas.get(position).getVideoTime());
        IShow.getInstance().showRound(context,datas.get(position).getBackgroundImg(),holder.back,false);
        holder.control.setBackgroundResource(R.drawable.live_icon_start);
        holder.title.setText(datas.get(position).getVideoTitle());
        holder.control.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClick(position);
            }
        });
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context,LiveDetailActivity.class);
                intent.putExtra("liveID",datas.get(position).getVideoID());
                intent.putExtra("type", LiveDetailActivity.LiveDetailType.VIDEO);
                context.startActivity(intent);
            }
        });
    }

    public void itemClick(int position){

    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public void upRereshView() {
        offset++;
        requestData();
    }

    @Override
    public void downRereshView() {
        offset = 0;
        this.datas.clear();
        requestData();
    }

    @Override
    public void setPullView(PullToRefreshListView listView){
        this.listView = listView;
    }

    @Override
    public void setDatas(final List<VideoBean> list) {
        if(list != null){
            this.datas.addAll(list);
        }
        if(datas.size() == 0){
            return;
        } else{
            send();
        }
    }

    @Override
    public void notifyChange(){
        if(listView != null){
            listView.onRefreshComplete();
        }
        listView.getRefreshableView().setAdapter(this);
        notifyDataSetChanged();
    }


    @Override
    public void requestData(){
        Call call = LVDataManager.getInstance().getVideos(offset, count, new Callback<ResultVideos>() {
            @Override
            public void onResponse(Call<ResultVideos> call, Response<ResultVideos> response) {
                    if(response.body() != null){
                        setDatas(response.body().getVideos());
                        tryCount = 0;
                    } else {
                        send();
                    }

            }

            @Override
            public void onFailure(Call<ResultVideos> call, Throwable t) {
                    send();
                    if(tryCount < 3){
                        requestData();
                        tryCount++;
                    }
            }
        });

        ((BaseNetWorkActivity)context).addCall(call);

    }

    public static class ViewHolder {
        public TextView title;
        public ImageView back;
        public TextView time;
        public ImageView control;
        public View rootView;
    }


}
